package com.utils {

import fairygui.Events;
import fairygui.GButton;
import fairygui.GTextInput;

import laya.events.Event;

public class BindInputButton {

	public var btn:GButton;
	public var array:Array;
	/** 当绑定的输入组件 内容修改后调用 */
	public var callback:Function;

	/**
	 *
	 * @param btn
	 * @param array
	 */
	public function BindInputButton(btn:GButton, array:Array) {
		this.btn = btn;
		this.array = array;

		var value:Object;
		var input:GTextInput;
		var gbtn:GButton;
		for (var i:int = 0; i < array.length; i++) {
			value = array[i];
			if (value is GTextInput) {
				input = value as GTextInput;
				input.on(Event.INPUT, this, changeHandler);
			} else if (value is GButton) {
				gbtn = value as GButton;
				gbtn.on(Events.STATE_CHANGED, this, changeHandler);
			}
		}
	}

	/** 检查一次状态 */
	public function check():void {
		changeHandler();
	}

	private function changeHandler():void {
		var enabled:Boolean = true; // 注释

		var value:Object;
		var input:GTextInput;
		var gbtn:GButton;
		for (var i:int = 0; i < array.length; i++) {
			value = array[i];
			if (value is GTextInput) {
				input = value as GTextInput;
				if (input.text.length == 0 || input.text == input.promptText) {
					enabled = false;
					continue;
				}
			} else if (value is GButton) {
				gbtn = value as GButton;
				if (gbtn.selected == false) {
					enabled = false;
					continue;
				}
			}
		}
		btn.enabled = enabled;
		if (callback != null) callback();
	}

}

}